package com.CommunityHub.kiana.service;


import com.CommunityHub.kiana.dao.*;
import com.CommunityHub.kiana.po.*;

import java.sql.SQLException;
import java.util.List;

public class MemberService {
    // DAO实例
    private final UserDAO userDAO = new UserDAO();
    private final MemberDetailDAO memberDetailDAO = new MemberDetailDAO();
    private final HouseInfoDAO houseInfoDAO = new HouseInfoDAO();
    private final ResidenceInfoDAO residenceInfoDAO = new ResidenceInfoDAO();
    private final ServiceRecordDAO serviceRecordDAO = new ServiceRecordDAO();

    // ================= 登录与账户管理 =================

    /**
     * 会员登录
     */

    /**
     * 获取会员详细信息
     */
    public MemberDetail getMemberDetail(int memberId) throws SQLException {
        return memberDetailDAO.findById(memberId);
    }

    /**
     * 更新会员信息
     */
    public boolean updateMemberInfo(MemberDetail member) throws SQLException {
        return memberDetailDAO.update(member);
    }

    // ================= 房产管理 =================

    /**
     * 获取所有可售房源
     */
    public List<HouseInfo> getAvailableHouses() throws SQLException {
        return houseInfoDAO.findByStatus(0); // 0表示可售状态
    }

    /**
     * 获取会员已购房产
     */
    public List<HouseInfo> getPurchasedHouses(int memberId) throws SQLException {
        return houseInfoDAO.findByBuyerId(memberId);
    }

    /**
     * 购买房产
     */
    public boolean purchaseHouse(int memberId, int houseId) throws SQLException {
        // 检查是否已购房
        if (hasPurchasedHouse(memberId)) {
            return false;
        }
        return houseInfoDAO.purchaseHouse(houseId, memberId);
    }

    /**
     * 检查会员是否已购房
     */
    public boolean hasPurchasedHouse(int memberId) throws SQLException {
        return houseInfoDAO.hasPurchasedHouse(memberId);
    }

    /**
     * 验证会员是否是房主
     */
    public boolean isHouseOwner(int memberId, int houseId) throws SQLException {
        return houseInfoDAO.isHouseOwner(houseId, memberId);
    }

    // ================= 入住人管理 =================

    /**
     * 获取会员的入住人列表
     */
    public List<ResidenceInfo> getOccupants(int memberId) throws SQLException {
        return residenceInfoDAO.findByMemberId(memberId);
    }

    /**
     * 根据房产获取入住人
     */
    public List<ResidenceInfo> getOccupantsByHouse(int houseId) throws SQLException {
        return residenceInfoDAO.findByHouseId(houseId);
    }

    /**
     * 添加入住人
     */
    public boolean addOccupant(ResidenceInfo occupant) throws SQLException {
        // 验证会员对该房产的所有权
        if (!isHouseOwner(occupant.getMemberId(), occupant.getHouseId())) {
            return false;
        }
        return residenceInfoDAO.addOccupant(occupant);
    }

    // ================= 服务管理 =================

    /**
     * 获取会员的服务记录
     */
    public List<ServiceRecord> getServiceHistory(int memberId) throws SQLException {
        return serviceRecordDAO.findByMemberId(memberId);
    }

    public List<ServiceResidence> getServiceHistoryById(int memberId) throws SQLException {
        return serviceRecordDAO.findByMemberById(memberId);
    }

    /**
     * 申请新服务
     */
    public boolean requestService(ServiceRecord record) throws SQLException {
        return serviceRecordDAO.addRecord(record);
    }

    // ================= 统一服务方法 =================

    /**
     * 获取会员完整信息
     * 包括基本信息和所有关联数据
     */
    public CompleteMemberInfo getCompleteMemberInfo(int memberId) throws SQLException {
        CompleteMemberInfo info = new CompleteMemberInfo();

        // 基本信息
        info.setMemberDetail(getMemberDetail(memberId));

        // 房产信息
        info.setPurchasedHouses(getPurchasedHouses(memberId));

        // 入住人信息
        info.setOccupants(getOccupants(memberId));

        // 服务记录
        info.setServiceHistory(getServiceHistory(memberId));

        return info;
    }
    // ================= 入住人管理 - 新增方法 =================

    /**
     * 根据入住人ID获取单个入住人信息
     */
    public ResidenceInfo getOccupantById(int residenceId) throws SQLException {
        return residenceInfoDAO.findById(residenceId);
    }

    /**
     * 更新入住人信息
     */
    public boolean updateOccupant(ResidenceInfo info) throws SQLException {
        // 验证会员对该房产的所有权
/*        if (!isHouseOwner(info.getMemberId(), info.getHouseId())) {
            return false;
        }*/
        return residenceInfoDAO.updateOccupant(info);
    }

    /**
     * 根据筛选条件查询房屋
     */
    public List<HouseInfo> filterHouses(String priceRange, String areaRange, String location)
            throws SQLException {
        return houseInfoDAO.findHousesByFilters(priceRange, areaRange, location);
    }


    /**
     * 内部类：会员完整信息包装类
     */
    public static class CompleteMemberInfo {
        private MemberDetail memberDetail;
        private List<HouseInfo> purchasedHouses;
        private List<ResidenceInfo> occupants;
        private List<ServiceRecord> serviceHistory;

        // Getters and Setters
        public MemberDetail getMemberDetail() {
            return memberDetail;
        }

        public void setMemberDetail(MemberDetail memberDetail) {
            this.memberDetail = memberDetail;
        }

        public List<HouseInfo> getPurchasedHouses() {
            return purchasedHouses;
        }

        public void setPurchasedHouses(List<HouseInfo> purchasedHouses) {
            this.purchasedHouses = purchasedHouses;
        }

        public List<ResidenceInfo> getOccupants() {
            return occupants;
        }

        public void setOccupants(List<ResidenceInfo> occupants) {
            this.occupants = occupants;
        }

        public List<ServiceRecord> getServiceHistory() {
            return serviceHistory;
        }

        public void setServiceHistory(List<ServiceRecord> serviceHistory) {
            this.serviceHistory = serviceHistory;
        }
    }
}